package com.aqielife.demo.handler;

import org.springframework.web.reactive.socket.WebSocketMessage;
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
import org.springframework.web.reactive.socket.client.WebSocketClient;
import reactor.core.publisher.Flux;

import java.net.URI;
import java.time.Duration;

/**
 * @author aqie
 * @date 2022/02/04 23:21
 * @desc
 */
public class WSClient {
  public static void main(final String[] args) {
    final WebSocketClient client = new ReactorNettyWebSocketClient();
    client.execute(URI.create("ws://localhost:8000/echo"), session ->
    session.send(Flux.just(session.textMessage("你好")))
    .thenMany(session.receive().take(1).map(WebSocketMessage::getPayloadAsText))
    .doOnNext(System.out::println)
    .then())
    .block(Duration.ofMillis(5000));
  }
}
